package my.mark.mybaibaoxiang.cooperationBehavior;

import my.mark.mybaibaoxiang.utils.MD5Util;
import my.mark.mybaibaoxiang.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 渠道对外交互接口。
 * 一般跟各种合作方，比方推广渠道的交互接口示例。
 * 为了保证数据的安全性，一般都会加【签名标识】进行校验。
 * @author wxming.
 * @date 2019-8-23.
 */
@RestController
@RequestMapping("/channel/repeat")
public class ChannelController {
    private final Logger logger = LoggerFactory.getLogger(ChannelController.class);
    private static final String APPLE_ID = "939582598";//应该是app应用商店里的ID号。
    private static final String SECRET_KEY = "882860293a7f991d9c2c6c175d190d33";//一个秘钥.


    /**
     * 爱盈利aso：排重接口
     * 0:该 IDFA 已下载过
     * 1:该 IDFA 没下载过
     *
     * @param appid appId
     * @param idfa 设备唯一标识
     */
    @RequestMapping("/aiyingli")
    public Map aiyingli(String appid,String idfa) {
        idfa = "test";
        Map result = new HashMap();
        try {
            String idfaStatus = "0";
            if (APPLE_ID.equals(appid) && !StringUtil.isBlank(idfa)) {
                //是否访问过
                String sql = "SELECT count(cb.id) num FROM cooperation_behavior cb WHERE cb.idfa=?";
                //long num = Db.queryLong(sql, idfa);

                //是否注册过
                String sql2 = "SELECT count(c.id) num FROM customer c WHERE c.deviceId = ?";
                //long num2 = Db.queryLong(sql2, idfa);

                /*if (num == 0 && num2 == 0) {
                    idfaStatus = "1";
                }*/
            }
            result.put(idfa, idfaStatus);
        } catch (Exception e) {
            logger.error("爱盈利aso排重接口失败");
        }
        return result;
    }

    /**
     * 爱盈利aso/零玖科技 点击接口
     * 返回JSON数据，格式为 { “code” : 0, ”result” : "ok"}
     * 当调用成功时，code=0, result固定为ok。
     * 如果调用失败，code为其他值，result为具体的错误信息 。
     *
     * 之后用户注册或者激活的方法里，会添加事件，
     * eg://app打开激活事件:
     * EventKit.postEvent(new ActivateEvent(appGather));
     * 事件监听会去触发回调。
     *
     * @param appid
     * @param idfa
     * @param ip
     * @param timestamp
     * @param sign 请求参数的 MD5 签名
     * @param callback 回调接口完整的 url urlencode 过的。eg：http://wall.jpmob.com/wall/advConfirm.do?appid=939582598&amp;idfa=f9b2528a1e53457facfa3d20322b0dca&amp;random=dio9xy0v0u79zwl5vd2bsd64pjcwr4
     */
    @RequestMapping("/aiyingliClick")
    public Map aiyingliClick(String appid, String idfa, String ip, String timestamp, String sign, String callback) {
        Map jsonResult = new HashMap();
        try {
            //校验签名
            String compareSign = MD5Util.MD5(timestamp + SECRET_KEY).toLowerCase();
            if (!sign.equals(compareSign)) {
                jsonResult.put("code", "-1");
                jsonResult.put("result", "签名失败");
                return jsonResult;
            }

            //校验参数
            if (!APPLE_ID.equals(appid) || StringUtil.isBlank(idfa)) {
                jsonResult.put("code", "-2");
                jsonResult.put("result", "参数不正确");
                return jsonResult;
            }

            //保留信息
            /*CooperationBehavior cooB = new CooperationBehavior();
            cooB.set("adid", "baidu_store");
            cooB.set("cid", "baidu_store");
            cooB.set("mac", "无");
            cooB.set("idfa", idfa);
            cooB.set("androidid1", "无");
            cooB.set("imei", "无");
            cooB.set("mac1", "无");
            cooB.set("aaid", "无");
            cooB.set("openudid", "无");
            cooB.set("androidid", "无");
            cooB.set("duid", "无");
            cooB.set("ua", "无");
            cooB.set("udid", "无");
            cooB.set("os", "1");
            cooB.set("ip", ip);
            cooB.set("ts", "无");
            cooB.set("callback", callback);
            cooB.set("status", 0);
            cooB.save();*/

            jsonResult.put("code", "0");
            jsonResult.put("result", "ok");
            return jsonResult;

        } catch (Exception e) {
            logger.error("爱盈利aso点击接口失败");
            jsonResult.put("code", "-4");
            jsonResult.put("result", "请求异常");
            return jsonResult;
        }
    }
}
